<template>
  <base-breadcrumb>
    <searchData
      :search-data="searchArr"
      @search="search"
    />
    <a-card :bordered="false">
      <div class="table-operator">
        <div class="pub-table-action-btn">
          <a-button
            v-btnPermission="'mm_013_add_01'"
            type="primary"
            icon="plus"
            @click="handleEdit(null,1)"
          >
            新增
          </a-button>

          <excel
            ref="exportSelect"
            file-name="物资用量确认单"
            is-click-generate="false"
          >
            <a-button
              slot="excelBtn"
              @click="exportSelect"
            >
              导出选中
            </a-button>
          </excel>
          <excel
            ref="exportAll"
            file-name="物资用量确认单"
            is-click-generate="false"
          >
            <a-button
              slot="excelBtn"
              @click="exportAll"
            >
              导出全部
            </a-button>
          </excel>
        </div>
        <div class="pub-table-select-count">
          <a-icon
            type="info-circle"
            theme="twoTone"
          />
          已选择{{ selectedRowKeys.length }}项
          <span style="padding-right: 10px;">
            总计：{{ totalSize }}
            待提交审核：{{ statistics.draft }}，
            审核中：{{ statistics.submitted }}，
            审核通过：{{ statistics.past }}，
            审核不通过：{{ statistics.rejected }}，
            已作废：{{ statistics.delete }}，
            已变更：{{ statistics.change }}
          </span>
        </div>
      </div>
      <a-table
        ref="table"
        :scroll="{x:1376}"
        :loading="loading"
        :pagination="false"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :columns="columns"
        :data-source="tableData"
        row-key="id"
      >
        <template
          slot="index"
          slot-scope="text, record, index"
        >
          {{ index + 1 }}
        </template>
        <template
          slot="auditStatusStr"
          slot-scope="text, record, index"
        >
          <list-status
            :status="record.auditStatus"
            :text="text"
          />
        </template>
        <div
          slot="operation"
          slot-scope="text, record, index"
          class="pub-table-list-action"
        >
          <span @click="handleEdit(record, 3)">查看</span>
          <span
            v-if="record.auditStatus === 'PAST'"
            @click="invalidResult(record)"
          >作废</span>
          <template v-if="record.auditStatus == 'DRAFT' || record.auditStatus == 'REJECTED'">
            <span @click="() => handleEdit(record, 2)">编辑</span>
            <span @click="() => handleDel(record.id)">删除</span>
          </template>
          <approval-button
            :id="record.id"
            :status="record.auditStatus"
            v-if="record.auditStatus === 'PAST' || record.auditStatus === 'SUBMITTED' || record.auditStatus === 'REJECTED' || record.auditStatus === 'REVOKEIN' || record.auditStatus === 'REVOKE' || record.auditStatus === 'ABOLISH' || record.auditStatus === '1' || record.auditStatus === '3' || record.auditStatus === 'COMMIT'|| record.auditStatus === '2' "
          />
        </div>
      </a-table>
      <pagination
        v-model="pageNo"
        :total="totalSize"
        :page-size.sync="pageSize"
        @change="handlePageChange"
        @showSizeChange="showSizeChange"
      />
    </a-card>
  </base-breadcrumb>
</template>
<script>
import { dosageList, dosageListDel, invalid,findListCount } from '@/api/material/acceptance/dosage'
import { selectOption } from '@/api/material/goods/goods'
import SelectSubCompany from '@/components/Select/SelectSubCompany'
import SelectProject from '@/components/Select/SelectProject'

export default {
	name:'a'+Date.now(),
    data: function() {
        return {
            loading: false,
            queryParam: {},
            pageNo: 1,
            pageSize: 10,
            totalSize: 0,
            searchArr: [
                {
                    name: '所属分公司',
                    valueKey: 'branchCompanyId',
                    type: SelectSubCompany
                },
                // {
                //     name: '项目名称',
                //     type: SelectProject,
                //     valueKey: 'projectId'
                // },
                {
                    name: '物资用量确认单编码',
                    value: 'code',
                    type: 'input'
                },
                {
                    name: '合同名称',
                    value: 'contractName',
                    type: 'input'
                },
                {
                    name: '合同编号',
                    value: 'contractCode',
                    type: 'input'
                },
                {
                    name: '合同类别',
                    value: 'contractTypeId',
                    type: 'select',
                    data: [],
                    request: () => selectOption({ type: 14 })
                },
                {
                    name: '工程名称',
                    value: 'constructionTypeId',
                    type: 'select',
                    data: [],
                    request: () => selectOption({ type: 18 })
                },
                {
                    name: '供应商名称',
                    value: 'supplier',
                    type: 'input'
                },
                {
                    name: '状态',
                    value: 'auditStatus',
                    type: 'select',
                    data: this.STATUS
                }
            ],
            columns: [
                {
                    title: '序号',
                    dataIndex: 'index',
                    scopedSlots: { customRender: 'index' },
                    width: 70
                }, {
                    title: '所属分公司',
                    dataIndex: 'branchCompanyName',
                    width: 150
                },
                {
                    title: '物资用量确认单编码',
                    dataIndex: 'code',
                    width: 160
                },
                {
                    title: '合同名称',
                    dataIndex: 'contractName',
                    width: 120
                },
                {
                    title: '合同编号',
                    dataIndex: 'contractCode',
                    width: 120
                },
                {
                    title: '合同类别',
                    dataIndex: 'contractTypeName',
                    width: 120
                },
                {
                    title: '工程名称',
                    dataIndex: 'constructionTypeName',
                    width: 120
                },
                {
                    title: '项目名称',
                    dataIndex: 'projectName',
                    width: 120
                },
                {
                    title: '供应商名称',
                    dataIndex: 'supplier',
                    width: 120
                },
                {
                    title: '状态',
                    width: this.STATUS_WIDTH,
                    dataIndex: 'auditStatusStr',
                    scopedSlots: { customRender: 'auditStatusStr' }
                },
                {
                    title: '操作',
                    dataIndex: 'operation',
                    width: 180,
                    fixed:'right',
                    scopedSlots: { customRender: 'operation' }
                }
            ],
            selectedRowKeys: [],
            statistics:{},
            selectId: [],
            selectedRows: [],
            chooseData: 0,
            tableData: [],
          pathName: this.$route.path,
        }
    },
    created() {
        this.list()
    },
    mounted: function() {
      this.list()
      this.$refs.exportSelect.tableToExcelFile(this.$refs.table)
        this.$refs.exportAll.tableToExcelFile(this.$refs.table)
      console.log('mounted-----------table的Path', this.pathName)
      // 监听详情页返回刷新列表
      //this.pathName取的值是this.$route.path
      this.$bus.$on('handleRefresh' + this.pathName, () => {
        console.log('--------------------收到了，刷新刷新刷新----------------',this.pathName)
        this.list()
      })
    },
  beforeDestroy() {
    console.log('beforeDestroy-------table的Path', this.pathName)
    this.$bus.$off('handleRefresh' + this.pathName)
  },
    methods: {
        exportSelect: function() {
            if (this.selectedRowKeys.length > 0) {
                this.$refs.exportSelect.setDataAndGenerate(this.selectedRows)
            } else {
                this.$message.warning('请选择需要导出的数据')
            }
        },
        exportAll: function() {
            // 参数：查询条件、导出的回调
            if (this.totalSize) {
                this.pageNo = 1
                this.list(false, this.$refs.exportAll.setDataAndGenerate)
            } else {
                this.$message.warning('没有可导出的数据')
            }
        },
        invalidResult: function(row) {
            this.invalidList(invalid, this.search, { id: row.id })
        },
        list(params, callback) {
            this.loading = true
            let _this = this
            let searchData = Object.assign({
                pageSize: callback ? _this.totalSize : _this.pageSize,
                pageNo: _this.pageNo
            }, params, this.queryParam)
             findListCount(searchData).then(res => {
                 if (res.code === this.SUCCESS_CODE) this.statistics = res.data
             })
            dosageList(searchData).then(res => {
                if (res.code === 200) {
                    if (callback) {
                        if (res.data.length) {
                            callback(res.data)
                        } else {
                            this.$message.warning('没有可导出的数据')
                        }
                        this.loading = false
                        return
                    }
                    _this.tableData = res.data
                    _this.totalSize = res.totalSize
                }
                this.loading = false
            })
        },
        search(val) {
            this.queryParam = val
            this.pageNo = 1
            this.list(val)
        },
        handlePageChange(pageNo, pageSize) {
            this.pageNo = pageNo
            this.list()
            this.selectedRowKeys = []
        },
        showSizeChange(pageNo, pageSize) {
            this.pageNo = 1
            this.pageSize = pageSize
            this.list()
        },
        onSelectChange(selectedRowKeys, selectedRows) {
            let _this = this
            _this.selectId = []
            selectedRows.map(item => {
                _this.selectId.push(item.id)
            })
            this.chooseData = selectedRowKeys.length
            _this.selectedRowKeys = selectedRowKeys
            _this.selectedRows = selectedRows
        },
        handleDel(id) {
            let _this = this
            let params = id ? [id] : _this.selectId
            _this.delList(dosageListDel, () => {
                _this.search()
                this.selectedId = []
                this.selectedRowKeys = []
                this.selectedRows = []
                this.editingKey = ''
            }, { ids: params })
        },
        handleEdit(record, state) {
            this.$router.push({
                path: '/legal/branch-contract/dosage/add',
                query: {
                    id: record && record.id || '',
                    state: state
                }
            })
        }
    }
}
</script>
